package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.EmployeeDTO;

import bizlogic.Employee;

/**
 * Servlet implementation class EmployeeEntryServlet
 */

public class EmployeeEntryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeEntryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		
		Employee emp = new Employee();
		EmployeeDTO empdto = new EmployeeDTO();
		empdto.setFirstName(request.getParameter("firstname"));
		empdto.setSurName(request.getParameter("surname"));
		empdto.setDesignation(request.getParameter("designation"));
		empdto.setRole(request.getParameter("role"));
		empdto.setTelNumber(request.getParameter("telephone"));
		empdto.setAddress(request.getParameter("address"));
		empdto.setEmail(request.getParameter("email"));
		empdto.setPassword(request.getParameter("password"));
		int id=0;
		String m=request.getParameter("manager");
		//System.out.println("manager===="+m);
		if(m == "" || m=="0")
		{
			empdto.setManagerID(id);
		}
		else
		{
			empdto.setManagerID(Integer.parseInt(m));
		}
		
		empdto.setGender(request.getParameter("gender"));
		empdto.setNric(request.getParameter("nric"));
		String ins = (String) request.getParameter("ins");
		Logger.getLogger(getClass().getName()).log(Level.INFO,
				"Insert Flag: " + ins);
		if (ins.equalsIgnoreCase("true")) {
			emp.insertEmployee(empdto);
		} else {
			emp.updateEmployee(empdto);
		}				
		
		ArrayList<EmployeeDTO> data = emp.findAllEmployee();
		request.setAttribute("employees", data);
		RequestDispatcher rd = request.getRequestDispatcher("/EmployeeListPage.jsp");
		try {			
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}		
}
